package com.example.shiwuyouproject.ui.fragment.my.activity
import android.content.Intent
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityRewardsandhonorsBinding
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.RewardsAndHonorsAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.RewardsAndHonorsViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.luck.picture.lib.config.PictureConfig
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import kotlin.collections.ArrayList
/**
 * 奖励荣誉
 */
class RewardsAndHonorsActivity :BaseVmActivity<ActivityRewardsandhonorsBinding, RewardsAndHonorsViewModel>(){
    override fun viewModelClass(): Class<RewardsAndHonorsViewModel> = RewardsAndHonorsViewModel::class.java

    override fun getViewBinding(): ActivityRewardsandhonorsBinding {
     return ActivityRewardsandhonorsBinding.inflate(layoutInflater)
    }
    override fun initView() {

    mViewModel.getRealNameAuthenticationData()
    }
    override fun setListener() {
        mBinding.tvBack.setOnClickListener { finish() }
        mBinding.btSava.setOnClickListener {
            onSava()
        }
    }
    private var myImgList:MutableList<String> = ArrayList()
    private var myImgAdapter: RewardsAndHonorsAdapter?=null
    private var addStatus=false
    private var myAdapterPosition=0
    override fun initData() {
        myImgList.add("")
        myImgAdapter=RewardsAndHonorsAdapter(myImgList)
        mBinding.recycler.adapter=myImgAdapter
         myImgAdapter?.setOnItemChildClickListener { adapter, view, position ->
                      if(view.id== R.id.imClose){
                          myImgList.removeAt(position)
                          val imgData = myImgList.filter { it != "" }
                          if(imgData.size==4){
                              myImgList.add("")
                          }
                          myImgAdapter?.notifyDataSetChanged()
         }else if(view.id==R.id.myHeaderImg) {
             val stImg: String = adapter.getItem(position) as String
             myAdapterPosition = position
             addStatus = !stImg.isNotEmpty()
             val imgData = myImgList.filter { it != "" }
             if (imgData.size != 5 || addStatus) {
                 QiNiuUpImageUtils.openCard(this)
             }
         }

         }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode==PictureConfig.REQUEST_CAMERA) {
            QiNiuUpImageUtils.postUpQiNiuFile(data,object : QiNiuUpImageUtils.onBackImageStatus {
                override fun onSuccess(key: String) {
                    if(addStatus){
                        myImgList.add(0,key)
                        if(myImgList.size==6){
                            myImgList.removeAt(myImgList.size-1)
                        }
                    }else{
                        myImgList[myAdapterPosition] = key
                    }
                    myImgAdapter?.notifyDataSetChanged()
                }
                override fun onFail() {
                    YUtils.hideLoading()
                    ToastUtil.show("图片上传失败，请检查网络连接")
                }
            })
        }
    }
    override fun observe() {
        mViewModel.getPostStatus.observe(this,androidx.lifecycle.Observer<Boolean>{
            YUtils.hideLoading()
            if(it) {
                ToastUtil.show("保存成功！")
                finish()
            }else{
                ToastUtil.show("保存失败！")
            }
        })
        mViewModel.getGenRenStatus.observe(this,androidx.lifecycle.Observer<PersonalInformationBean>{
            mBinding.text.setText(it.honor)
           if( it.honorimg.size==5){
               myImgList.removeAt(0)
               myImgList.addAll(it.honorimg)
           }else{
               myImgList.addAll(0,it.honorimg)
           }
            myImgAdapter?.notifyDataSetChanged()
        })
    }
    /**
     * 保存
     */
   private  fun onSava(){
         if(mBinding.text.text.isEmpty()){
             ToastUtil.show("请输入荣誉介绍！")
             return
         }
        YUtils.showLoading(this, "保存中...")
        val imgData:MutableList<String> = myImgList.filter { it != "" } as MutableList<String>
        mViewModel.preservationCard(mBinding.text.text.toString(),imgData)
   }
}